﻿using DSPL.Common.Classes;
using DSPL.Contracts;
using DSPL.Contracts.Command;
using DSPL.Contracts.ViewModel;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Input;

namespace DSPL.Common.ViewModel
{
    public class SortWindowViewModel : BaseViewModel
    {
        private ICommand formLoad;
        Window parentWindow;
        private ICommand closeCommand;
        private ICommand selectCommand;
        private ObservableCollection<ColumnValueProperties> orderByList;
        private ObservableCollection<ColumnValueProperties> _ColumnList;
        private ObservableCollection<SortStringProperties> sortData;
        private string sortString;
        private OrderbyOptions options = OrderbyOptions.DefaultOrder;                 
        

        public SortWindowViewModel(ObservableCollection<ColumnValueProperties> columnList)
        {
            ColumnList = columnList;
            OrderByList = new ObservableCollection<ColumnValueProperties>();
            OrderByList.Add(new ColumnValueProperties() { DispalyMember = "Ascending Order", ValueMember="ASC" });
            OrderByList.Add(new ColumnValueProperties() { DispalyMember = "Descending Order",ValueMember="DESC" });
            SortData = new ObservableCollection<SortStringProperties>() { new SortStringProperties() };
            OrderbyOption = OrderbyOptions.NoOrder;
        }

        public ObservableCollection<ColumnValueProperties> ColumnList
        {
            get
            {
                return _ColumnList;
            }
            set
            {
                _ColumnList = value;
                OnPropertyChanged("ColumnList");
            }
        }
        
        public ObservableCollection<ColumnValueProperties> OrderByList
        {
            get
            {
                return orderByList;
            }
            set
            {
                orderByList = value;
                OnPropertyChanged("OrderByList");
            }
        }

        public ObservableCollection<SortStringProperties> SortData
        {
            get
            {
                return sortData;
            }
            set
            {
                sortData = value;
                OnPropertyChanged("SortData");
            }
        }

        public ICommand FormLoad
        {
            get
            {
                if (this.formLoad == null)
                    this.formLoad = new RelayCommand(FormLoadExecuted);
                return this.formLoad;
            }
            set { formLoad = value; }
        }

        public ICommand CloseCommand
        {
            get
            {
                if (this.closeCommand == null)
                    this.closeCommand = new RelayCommand(CloseCommandExecuted);
                return this.closeCommand;
            }
            set { closeCommand = value; }
        }

        public ICommand SelectCommand
        {
            get
            {
                if (this.selectCommand == null)
                    this.selectCommand = new RelayCommand(SelectCommandExecute);
                return this.selectCommand;
            }
            set { selectCommand = value; }
        }

        public string SortString
        {
            get { return sortString; }
            set { sortString = value; }
        }

        private void FormLoadExecuted(object obj)
        {
            if (obj != null)
            {
                parentWindow = (Window)obj;
                parentWindow.Title = "Sorting Options";
            }
        }

        public OrderbyOptions OrderbyOption
        {
            get { return options; }
            set { options = value;
            OnPropertyChanged("OrderbyOption");
            }
        }
        
        private void CloseCommandExecuted(object obj)
        {
            var window = obj as Window;
            if (window != null)
                window.Close();
        }

        private void SelectCommandExecute(object obj)
        {
            if (SortData.Count != 0 && SortData != null)
            {
                SortString = String.Empty;

                foreach (SortStringProperties item in SortData)
                {
                    if (SortString.Trim().Length>0)
                    {
                        SortString = SortString + ", ";
                    }
                    if(item.SortKey!=null)
                    {
                        if(item.SortOrder==null)
                            SortString = SortString + " " + item.SortKey + " Asc";
                        else
                            SortString = SortString + " " + item.SortKey + " " + item.SortOrder;
                    }
                }
            }
            if (parentWindow != null)
                parentWindow.Close();
        }
    }


    public class SortStringProperties : BaseViewModel
    {
        private string sortKey;
        private object sortOrder;
        private ColumnValueProperties selectedSortKey;
        private ColumnValueProperties selectedSortOrder;

        public string SortKey
        {
            get
            {
                return sortKey;
            }
            set
            {
                sortKey = value;
                OnPropertyChanged("FilterKey");
            }
        }

        public object SortOrder
        {
            get { return sortOrder; }
            set
            {
                sortOrder = value;
                OnPropertyChanged("FilterValue");
            }
        }

        public ColumnValueProperties SelectedSortKey
        {
            get { return selectedSortKey; }
            set
            {
                selectedSortKey = value;
                OnPropertyChanged("SelectedSortKey");
            }
        }

        public ColumnValueProperties SelectedSortOrder
        {
            get { return selectedSortOrder; }
            set
            {
                selectedSortOrder = value;
                OnPropertyChanged("SelectedSortOrder");
            }
        }

        public SortStringProperties()
        {
            
        }

        public SortStringProperties(string sortKey)
        {
            this.SortKey = sortKey;
        }
        
        public SortStringProperties(string sortKey, string sortOrder)
        {
            this.SortKey = sortKey;
            this.SortOrder = sortOrder;
        }
        
    }

}
